<template>
  <div>
    <el-form ref="form" :model="formData" label-width="120px" v-loading="loading" :rules="rules">
      <el-form-item label="项目名称" prop="username">
        <el-input v-model="formData.username" placeholder="项目名称"></el-input>
      </el-form-item>
      <el-form-item label="项目建设方" prop="project_company">
        <el-input
          v-model="formData.project_company"
          placeholder="项目建设方"
        ></el-input>
      </el-form-item>
      <el-form-item label="承建单位" prop="company_name">
        <el-input
          v-model="formData.company_name"
          placeholder="承建单位"
        ></el-input>
      </el-form-item>
      <el-form-item label="管理员电话" prop="manager_tel">
        <el-input
          v-model="formData.manager_tel"
          placeholder="此电话是APP注册用户，更换其它用户表示修改项目拥有人"
        ></el-input>
      </el-form-item>
      <el-form-item label="管理员姓名" prop="manager_name">
        <el-input
          v-model="formData.manager_name"
          placeholder="管理员姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="维护单位" prop="maintain_company">
        <el-input
          v-model="formData.maintain_company"
          placeholder="维护单位"
        ></el-input>
      </el-form-item>
      <el-form-item label="主维护人电话" prop="maintain_tel">
        <el-input
          v-model="formData.maintain_tel"
          placeholder="此电话是APP注册用户，更换其它用户表示修改项目拥有人"
        ></el-input>
      </el-form-item>
      <el-form-item label="主维护人姓名" prop="maintain_name">
        <el-input
          v-model="formData.maintain_name"
          placeholder="主维护人姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="所在省份" prop="province_id">
        <el-select
          @change="getCity"
          v-model="formData.province_id"
          placeholder="所在省份"
        >
          <el-option
            v-for="(item, index) in province"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在城市" prop="city_id">
        <el-select
          @change="getArea"
          v-model="formData.city_id"
          placeholder="所在城市"
        >
          <el-option
            v-for="(item, index) in city"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在市辖区" prop="area_id">
        <el-select
          @change="getTown"
          v-model="formData.area_id"
          placeholder="所在市辖区"
        >
          <el-option
            v-for="(item, index) in area"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在乡镇" prop="town_id">
        <el-select v-model="formData.town_id" placeholder="所在乡镇">
          <el-option
            v-for="(item, index) in town"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="详细地址" prop="project_address">
        <el-input
          v-model="formData.project_address"
          placeholder="详细地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="formData.status" placeholder="状态">
          <el-option key="0" value="0" label="无效"></el-option>
          <el-option key="1" value="1" label="有效"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div style="display: flex; justify-content: flex-end;">
      <el-button type="default" @click="cancel">取消</el-button>
      <el-button type="primary" @click="submit">提交</el-button>
    </div>
  </div>
</template>

<script>
import { area } from '@/api/common.js';
import { addproject } from '@/api/user/project.js';
export default {  
  data() {
    return {      
      formData: {
        username: '',
        project_company: '', //项目建设方
        company_name: '', //承建单位
        manager_tel: '',
        manager_name: '',
        maintain_company: '',
        maintain_tel: '',
        maintain_name: '',
        province_id: '',
        city_id: '',
        area_id: '',
        town_id: '',
        project_address: '',
        status: '',
      },
      province: {},
      city: {},
      area: {},
      town: {},
      loading: false,

      rules: {
        username: [
          { required: true, message: '请输入项目名称', trigger: 'blur' },
        ],
        project_company: [
          { required: true, message: '请输入项目建设方', trigger: 'blur' },
        ],
        company_name: [
          { required: true, message: '请输入承建单位', trigger: 'blur' },
        ],
        manager_tel: [
          { required: true, message: '请输入管理员电话', trigger: 'blur' },
        ],
        manager_name: [
          { required: true, message: '请输入管理员姓名', trigger: 'blur' },
        ],
        maintain_company: [
          { required: true, message: '请输入维护单位', trigger: 'blur' },
        ],
        maintain_tel: [
          { required: true, message: '请输入主维护人电话', trigger: 'blur' },
        ],
        maintain_name: [
          { required: true, message: '请输入主维护人姓名', trigger: 'blur' },
        ],
        province_id: [
          { required: true, message: '请选择所在省份', trigger: 'blur' },
        ],
        city_id: [
          { required: true, message: '请选择所在城市', trigger: 'blur' },
        ],
        area_id: [
          { required: true, message: '请选择所在市辖区', trigger: 'blur' },
        ],
        town_id: [
          { required: true, message: '请选择所在乡镇', trigger: 'blur' },
        ],
        project_address: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
        ],
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }],
      },
    };
  },
  created() {
    this.getProvince();
  },
  methods: {
    getProvince() {
      area({ pid: 0 }).then((res) => {
        this.province = res.data;
        console.log(res, '......');
      });
    },
    getCity() {
      this.formData.city_id = '';
      this.formData.area_id = '';
      this.formData.town_id = '';
      area({ pid: this.formData.province_id }).then((res) => {
        this.city = res.data;
      });
    },
    getArea() {
      this.formData.area_id = '';
      this.formData.town_id = '';
      area({ pid: this.formData.city_id }).then((res) => {
        this.area = res.data;
      });
    },
    getTown() {
      this.formData.town_id = '';
      area({ pid: this.formData.area_id }).then((res) => {
        this.town = res.data;
      });
    },
    submit() {
      this.loading = true;
      this.$refs.form.validate((valid) => {
        if (valid) {
          addproject(this.formData).then((res) => {
            this.loading = false;
            this.$message.success("添加成功")            
            this.$emit('update:visible', false)
            this.$emit("success")
          }).finally(res=>{            
            this.loading =false;
          });
        }else{
          this.loading = false;
        }
      });
    },
    cancel(){
      this.$emit('update:visible', false)
    }
  },
};
</script>

<style scoped></style>
